#include <SoftwareSerial.h>
#include <STM32FreeRTOS.h>
/*----------------------------GPIO接口分布--------------------------------
H1 -> PB3
H2 -> PB4
H3 -> PB5 -> 震动
H4 -> PA1 -> 温湿度
H5 -> PA6 -> 光敏
H6 -> PA7
H7 -> PB0 
H8 -> PA5 -> 烟雾感
########################################################
-------------------ADC接口PA0-7、PB0、PB1;
########################################################
------------------------------------------------------------------------*/

//串口1北斗接口
SoftwareSerial mySerial1(PA10,PA9); //(rx,tx)
//串口3调试输出接口
//SoftwareSerial mySerial3(PB11,PB10); //(rx,tx)


#define RUN_LED PB13

#define test PA0

/*---------------------
        参数设置
------------------------*/
//温湿度
#include "DHT.h"
DHT dht(PA1, DHT11);
int sd = 0;
int wd = 0;
//烟雾探测
#define YGA PA7
#define YGD PB5
unsigned int yg = 0;
//光敏探测
#define GMA PA2
#define GMD PB8
int gm = 0;//有光0,1无光;
//震动
int zd = 0;

//定义北斗通信函数
bool ml(){
    char c[15];
    mySerial1.print("+++ exit transparent transmission\r\n");
    delay(2000);
    int i = 0;
    while(mySerial1.available() > 0){
      c[i] = mySerial1.read();
      i++;
    }
    if(c[11]=='O' || c[7]=='O'){
      mySerial1.println("ml OK");
      return false;
   }else{
     return true;
     mySerial1.println("ml ERR");
   }
}

bool xh(){
    char c[25];
    mySerial1.print("AT+CSQ?\r\n");
    delay(2000);
    int i = 0;
    int b = 0;
    while(mySerial1.available() > 0){
      c[i] = mySerial1.read();
      i++;
    }
    mySerial1.println(c);
    for(int a = 5; a <=23;a = a+2){
      int temp = c[a] - '0';
      if(temp >= 3){
        b++;
      }
    }
    if(b >= 2){
      mySerial1.println("xh OK");
      return false;
    }else{
      mySerial1.println("xh err");
      return true;
    }
}

void send(){
  digitalWrite(RUN_LED,HIGH);
  char c[70];
  /*------------ sd->湿度 wd->温度 gg->光敏 yg->烟感 zd->震动 -------------------*/
  mySerial1.printf("AT+SEND=193664,\"sd:%d,wd:%d,gm:%d,yg:%d\"\r\n",sd,wd,gm,yg);
  delay(2000);
  int i = 0;
  while(mySerial1.available() > 0){
    c[i] = mySerial1.read();
    i++;
  }
  digitalWrite(RUN_LED,LOW);
  if(c[5]=='O'){
    mySerial1.println("SEND OK");
  }
  if(c[5]=='E'){
    mySerial1.println("SEND ERR");
  }
  if(c[0]=='C'){
      mySerial1.println("reboot");
  }
}

//定义感知模块函数
void gz(void* arg){
  UNUSED(arg);
  while(1){
    //--------------获取烟雾数据
    yg = analogRead(YGA);
    //--------------获取温度、湿度
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    if (isnan(h) || isnan(t)) {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
    }
    sd = (int)h;
    wd = (int)t;
    //------------------获取光敏数据
    gm = digitalRead(GMD);
    delay(5000);
  }
}

//北斗发射数据线程
void bd(void* arg){
  UNUSED(arg);
//使用多线程时留意使用死循环
  while(1){
    delay(5000);
    send();
  }
}

void setup() {
  portBASE_TYPE s1,s2;
  //定义数字信号接口
  pinMode(RUN_LED,OUTPUT);
  pinMode(YGD,INPUT);
  pinMode(GMD,INPUT);
  mySerial1.begin(9600);
  while(!mySerial1){
  }
  while(ml()){
    delay(2000);
  }
  while(xh()){
    delay(2000);
  }
  dht.begin();
  //创建线程
  s1 = xTaskCreate(gz,NULL,configMINIMAL_STACK_SIZE,NULL,1,NULL);
  s2 = xTaskCreate(bd,NULL,configMINIMAL_STACK_SIZE,NULL,1,NULL);
  if(s1 != pdPASS || s2 != pdPASS){
    //这里输出报错
    while(1);
  }
  //启动线程
  vTaskStartScheduler();
  while(1);
}

void loop() {
  //{ "type": "mysql", "hostname": "47.57.233.174", "database": "dlminecom", "username": "dlminecom", "password": "ZEhxSKE4ebpYyr3s", "hostport": "", "dsn": "mysql:dbname=dlminecom;host=47.57.233.174;charset=utf8", "params": { "12": true }, "charset": "utf8", "prefix": "dbb_", "debug": true, "deploy": 0, "rw_separate": false, "master_num": 1, "slave_no": "", "fields_strict": true, "resultset_type": "array", "auto_timestamp": false, "sql_explain": false, "builder": "", "query": "\\think\\db\\Query" }
}

